package com.qiangou.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.entity.AdvMes;
import com.qiangou.service.AdvMesService;
import com.qiangou.service.CreditService;

import com.rsa.Base64;
import com.rsa.RSA;
import com.util.FTPUtil;

/**
 * @author eastmelon
 */

@Component
@Path("/v2")
public class JerseyControllerone {
	
	@Resource(name="creditService")
	private CreditService creditService;
	
	@Resource(name = "AdvMesService")
	private AdvMesService AdvMesService;
	
//	@Resource(name="TagsService")
//	private TagsService TagsService;
//	/*
//	 * 根据标签返回银行卡id
//	 * */
//	@GET
//	@Path("/getCreditIds")
//	@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML})
//	public Response getCreditIds(@QueryParam("cardId") String cardId) {
//		List<Integer> ids = new ArrayList<Integer>();
//		Map<String,Object> data=new HashMap<String,Object>();
//		String data1 = String.valueOf(ids);
//		data1 = Base64.encode(data1.getBytes());
//		String data2 = RSA
//				.sign(data1,
//						"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKWmOMF5zQBqc8xA41zq9BKHI8GUdZdJR8qP57mv8y2N7T/TD1YeNEx3UH7cSWpsZpdzV5BWKAhH7sTOWfhIU+EhrXrWbQWAndWcT2+ZDYlI0ctJ5Bo1gM0MEU01+g3mhOf70I9DJrYGvoVYV815m+F46bjq8qVEL06zZZLEKCTPAgMBAAECgYEAjZl3rrvFp/NXpWRadtVJaoUm5ZVYp8g2nEtDVJG5mFlYU1TCKWWMY0kjAC6ie1zKnfA1C+b6NYn36zhR5FE/kTSwUYT1P6INT4rD7JUEiwE8hi4MTvWIDCyqeUmb2H+abHpBo9VZymmh5wmwRTi1PgPQGTDq5uP519lgD00DzEECQQDZzHPSvgy0GztmD6Uip1mHDI9j7syIEVCEPtuIsNzH63GQdR5jLH7hZn0WRXEgrZa+f3gKZnFIew+lj6Ip1lPRAkEAwrQrwe6dcioJHdc0PsW/In5TOBTY+ppVKhrkJ6x9UOzZT/BYuXUFVJL8kiKGIK0wihOzMhHK57HjoN5fT5w2nwJBAJ5D4npmXgbWrxAYGFCZOQZYyy28DmZl5pNitdabZqPj5A8r/Bvm7oBOIGF5rp4nZh4htJIiJPmdax5MxHMQarECQH8yMPvqpJT2fSovcwQnL2ybVkZm6DEfLc/p7W81slBxyq38eBoAJtFPjQzy3Ojv+6vYntJw6Ttf7TMk0uMxTEUCQDw1ZXU5jiKoktSYjHjFL2EfqtfT9F8xTHVyaruKL+R67Z0OvxMNlM0j77ADUS/BAFlwF2bCsSkMT3PLvcNtpPk=");
//		data.put("data",data1);
//		data.put("sign", data2);
//		return Response.status(200).entity(String.valueOf(JSONObject.toJSON(data))).build();
//	}
//	
//	@GET
//	@Path("/getCreditCount")
//	@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML})
//	public Response getCreditCount(@QueryParam("types") String type) {
//		if(type.equals("")){
//			return Response.status(200).entity(0).build();
//		}
//		List<String> tags = new ArrayList<String>();
//		String[] types = type.split(",");
//		tags = creditService.getTagNames(types);
//		int count = creditService.getCrerditCount(tags);
//		return Response.status(200).entity(String.valueOf(count)).build();
//	}
//	
	
	
	@GET
	@Path("/getAdMes")
	@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML})
	public Response getMessage1(@QueryParam("advid") int advid) {
		AdvMes s=new AdvMes();
		      s.setAdvid(advid);
		  AdvMes r=AdvMesService.getAdvMes(s);
		return Response.status(200).entity(r).build();
	}
	
	
	/**
	 * 得到所有广告效果反馈信息
	 * 
	 * @return
	 */
	@GET
	@Path("/getAllAdMes")
	@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
	public Response getAllMessageone() {
		// List<AdvMes> list = new ArrayList<AdvMes>();
		// Map<String, Object> data = new HashMap<String, Object>();
		AdvMes s = new AdvMes();
//		s.setDjl("1");

		List<AdvMes> advmes = AdvMesService.findAllAdvMes(s);
		

		return Response.status(200).entity(advmes).build();
		
		
	}

	/**
	 * 得到所有广告效果反馈信息 加密
	 * 
	 * @return
	 */
	// @GET
	// @Path("/getAllAdMes")
	// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
	// public Response getAllMessageone() {
	// //List<AdvMes> list = new ArrayList<AdvMes>();
	// Map<String, Object> data = new HashMap<String, Object>();
	// AdvMes s = new AdvMes();
	// s.setDjl(1);
	//
	// List<AdvMes> advmes=AdvMesService.findAllAdvMes(s);
	// String data1=String.valueOf(advmes);
	// data1 = Base64.encode(data1.getBytes());
	// String data2 = RSA
	// .sign(data1,
	// "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKWmOMF5zQBqc8xA41zq9BKHI8GUdZdJR8qP57mv8y2N7T/TD1YeNEx3UH7cSWpsZpdzV5BWKAhH7sTOWfhIU+EhrXrWbQWAndWcT2+ZDYlI0ctJ5Bo1gM0MEU01+g3mhOf70I9DJrYGvoVYV815m+F46bjq8qVEL06zZZLEKCTPAgMBAAECgYEAjZl3rrvFp/NXpWRadtVJaoUm5ZVYp8g2nEtDVJG5mFlYU1TCKWWMY0kjAC6ie1zKnfA1C+b6NYn36zhR5FE/kTSwUYT1P6INT4rD7JUEiwE8hi4MTvWIDCyqeUmb2H+abHpBo9VZymmh5wmwRTi1PgPQGTDq5uP519lgD00DzEECQQDZzHPSvgy0GztmD6Uip1mHDI9j7syIEVCEPtuIsNzH63GQdR5jLH7hZn0WRXEgrZa+f3gKZnFIew+lj6Ip1lPRAkEAwrQrwe6dcioJHdc0PsW/In5TOBTY+ppVKhrkJ6x9UOzZT/BYuXUFVJL8kiKGIK0wihOzMhHK57HjoN5fT5w2nwJBAJ5D4npmXgbWrxAYGFCZOQZYyy28DmZl5pNitdabZqPj5A8r/Bvm7oBOIGF5rp4nZh4htJIiJPmdax5MxHMQarECQH8yMPvqpJT2fSovcwQnL2ybVkZm6DEfLc/p7W81slBxyq38eBoAJtFPjQzy3Ojv+6vYntJw6Ttf7TMk0uMxTEUCQDw1ZXU5jiKoktSYjHjFL2EfqtfT9F8xTHVyaruKL+R67Z0OvxMNlM0j77ADUS/BAFlwF2bCsSkMT3PLvcNtpPk=");
	// data.put("data", data1);
	// data.put("sign", data2);
	// return Response.status(200)
	// .entity(String.valueOf(JSONObject.toJSON(data))).build();
	// }

	/**
	 * 根据路径参数插入到数据库
	 */

	@GET
	@Path("/save")
	@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
	public Response insertData(@QueryParam("adv") String adv) {
		//首先获取接口传过来在数据，解析参数，如果要JSON格式  JSON转换成Map，或者说有对于bean，
		//直接转换设置到bean里，百度json到实体类例子。
		AdvMes am  = new AdvMes();
		Map<String,Object> map = JSON.parseObject(adv, Map.class);
		am.setAdvid(Integer.valueOf(map.get("advid").toString()));
		am.setSml(map.get("sml").toString());
		am.setDjl(map.get("djl").toString());
		//值设置到bean里之后，直接把bean作为参数，insert到数据库里
		 AdvMesService.save(am);
		
		//插入成功之后，返回一个成功在标记，json格式
//		String result = "";
//		if(num>0){
//			result="1";
//		}else{
//			result="0";
//		}
//		AdvMes am = new AdvMes();
//		am.setSml("sml");
//		am.setDjl("djl");
//		List<AdvMes> data=AdvMesService.insertData(am);

//		System.out.println("接受数据成功");
//		Map m = new HashMap();
//		m.put("result", result);
		return Response.status(200).entity("{flag:ok}").build();
	}
	
	
	
	
//	/**
//	 *通过卡号得到标签
//	 */
//
//	@GET
//	@Path("/getTagsByCreditid")
//	@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
//	public Response getTagsByCreditid(@QueryParam("id") int id) {
//	//	String.valueOf(id);
//		 List<Object>  list=new ArrayList<Object>();
//		
//		 list=creditService.getTagsByCreditid(id);
//	
//		return Response.status(200).entity(list).build();
//	}
	
	
	/**
	 * 读取FTP服务器将得到的数据插入数据库
	 * @return
	 */
	
	@GET
	@Path("/savesj")
	@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
	public Response insertData1() {
		
		List<AdvMes>  am=new ArrayList<AdvMes>();
		FTPUtil  fu=new FTPUtil();
		String  data=fu.readConfigFileForFTP("ysq","ysq320925","/FTP下载","172.16.31.63", 21,"test.json");
		am=JSON.parseArray(data, AdvMes.class);
		for (AdvMes advMes : am) {
			AdvMesService.save(advMes);
		}
		
		return Response.status(200).entity("{flag:ok}").build();
	}
	
}
